<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="券名称：" name="title">
				<a-input v-model:value="formData.title" placeholder="请输入券名称" allow-clear />
			</a-form-item>
			<a-form-item label="券类型：" name="couponType">
				<a-select
					v-model:value="formData.couponType"
					:options="couponTypeOptions"
					placeholder="请选择券类型"></a-select>
			</a-form-item>
			<a-form-item label="券描述：" name="description">
				<a-input v-model:value="formData.description" placeholder="描述信息，用于向用户展示优惠券"/>
			</a-form-item>
			<a-form-item label="面额类型：" name="valueType">
				<a-radio-group
					v-model:value="formData.valueType"
					:options="valueTypeOptions"></a-radio-group>
			</a-form-item>
			<a-form-item label="券面额：" name="value">
				<a-input-number
					v-model:value="formData.value"
					style="width: 160px;"
					:min="0"
					placeholder="请输入券面额">
					<template #addonAfter>
						{{ formData.valueType === 'AMOUNT' ? '元' : '折'}}
					</template>
				</a-input-number>
			</a-form-item>
			<a-form-item label="使用限制：" name="useCondition">
				<a-input-number
					style="width: 160px;"
					:min="0"
					v-model:value="formData.useCondition">
					<template #addonAfter>元</template>
				</a-input-number>
			</a-form-item>
			<a-form-item label="用户限制：" name="userType">
				<a-radio-group
					v-model:value="formData.userType"
					:options="userTypeOptions"></a-radio-group>
			</a-form-item>
			<a-form-item label="发券数量：" name="issueCount">
				<a-input-number :min="0" v-model:value="formData.issueCount" style="width: 160px"/>
			</a-form-item>
			<a-form-item label="有效期：" name="effectDateType">
				<a-radio-group
					v-model:value="formData.effectDateType">
					<a-radio value="FIXED">固定日期</a-radio>
					<a-radio value="RECEIVE">有效天数</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item v-if="formData.effectDateType === 'FIXED'">
				<a-date-picker format="YYYY-MM-DD" v-model:value="formData.useStartTime"></a-date-picker>
				至
				<a-date-picker format="YYYY-MM-DD" v-model:value="formData.useEndTime"></a-date-picker>
			</a-form-item>
			<a-form-item v-else>
				<a-input-number style="width: 160px" :min="0" v-model:value="formData.effectDays">
					<template #addonAfter>天</template>
				</a-input-number>
			</a-form-item>
			<a-form-item label="发券方式：" name="issueType">
				<a-radio-group
					v-model:value="formData.issueType">
					<a-radio value="0">领券后发放</a-radio>
					<a-radio value="2">消费后发放</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item v-if="formData.issueType === '2'">
				<a-input-number style="width: 160px" :min="0" v-model:value="formData.issueCondition">
					<template #addonAfter>元</template>
				</a-input-number>
			</a-form-item>
			<a-form-item label="使用说明：" name="note">
				<a-textarea show-count v-model:value="formData.note" :rows="4"/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmCardTypeForm">
	import { message } from 'ant-design-vue'
	import dayjs from "dayjs"
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			status: '0',
			issueType: '0',
			useType: 'ALL',
			goodScope: 'ALL',
			issueCondition: 0,
			valueType: 'AMOUNT',
			effectDateType: 'FIXED',
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.issueType += ''
			if (formData.value.useStartTime) {
				formData.value.useStartTime = dayjs(formData.value.useStartTime)
			}
			if (formData.value.useEndTime) {
				formData.value.useEndTime = dayjs(formData.value.useEndTime)
			}
			if (formData.value.startTime) {
				formData.value.startTime = dayjs(formData.value.startTime)
			}
			if (formData.value.endTime) {
				formData.value.endTime = dayjs(formData.value.endTime)
			}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入卡编码')],
		status: [required('请选择卡状态')],
		cardType: [required('请选择卡类型')],
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	let couponTypeOptions = proxy.$TOOL
		.dictTypeList('COUPON_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let valueTypeOptions = tool
		.dictTypeList('COUPON_VALUE_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let userTypeOptions = tool
		.dictTypeList('COUPON_USER_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let storeOptions = tool
		.tableList('ORG_CATEGORY')
		.filter(item => item.category === 'STORE')
		.map(item => {
			return {
				value: item['code'],
				label: item['name']
			}
		})
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = parameterChanges(formData.value)
				if (param.startTime) {
					param.startTime = dayjs(param.startTime).format('YYYY-MM-DD')
				}
				if (param.endTime) {
					param.endTime = dayjs(param.endTime).format('YYYY-MM-DD')
				}
				if (param.useStartTime) {
					param.useStartTime = dayjs(param.useStartTime).format('YYYY-MM-DD')
				}
				if (param.useEndTime) {
					param.useEndTime = dayjs(param.useEndTime).format('YYYY-MM-DD')
				}
				submitLoading.value = true
				crmApi.couponActivitySubmitForm(param, param.id).then(() => {
					visible = false
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}
	const parameterChanges = (data) => {
		return Object.assign({}, data)
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
